package com.design.softwaredesign.controller;

import com.design.softwaredesign.front.UserPhoneAndPassword;
import com.design.softwaredesign.front.UserInfo;
import com.design.softwaredesign.front.UserRegisterInfo;
import com.design.softwaredesign.service.UserService;
import com.design.softwaredesign.utils.SendAliMessageUtils;
import com.design.softwaredesign.vo.DataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author :WaiterXiaoYY
 * @description: 用户操作-登录验证，注册
 * @data :2020/9/29 16:58
 */

@Api(description = "用户控制层")
@RestController
@RequestMapping("/web")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public DataVo userLogin(@RequestBody UserPhoneAndPassword userPhoneAndPassword) {
        return userService.userLogin(userPhoneAndPassword.getPhone(), userPhoneAndPassword.getPassword());
    }

    @GetMapping("/sendcode")
    public DataVo sendCode(String phone, HttpServletRequest request) {
        DataVo dataVo = new DataVo();
        try {
//            String code = SendWangYiMessageUtils.sendMess(phone);
            int code = 0;
            // 检验电话号码是否被注册过了
            if(userService.checkPhone(phone) == 1) {
                code = SendAliMessageUtils.sendMess(phone);
                if(code == -1) {
                    dataVo.setMsg("验证发送出错，请重试");
                    throw new Exception("Error:SendCode Error!");
                }
            }
            else {
                dataVo.setMsg("手机号已被注册过");
                throw new Exception("Error:Phone Error!");
            }

//            HttpSession session = request.getSession();
//            session.setAttribute(phone, String.valueOf(code));

            userService.updateCode(phone, String.valueOf(code));
            dataVo.setMsg("验证码发送成功");
            dataVo.setCode(200);
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("验证码发送失败");
        } finally {
            return dataVo;
        }
    }

    @PostMapping("/register")
    public DataVo userRegister(@RequestBody UserRegisterInfo userRegisterInfo, HttpServletRequest request) {
        DataVo dataVo = new DataVo();
        // 从session中取出手机号对应的code
//        String code = (String) request.getSession().getAttribute(userRegisterInfo.getPhone());
        String code = userService.getPhoneCode(userRegisterInfo.getPhone());
        if(userRegisterInfo.getCode() == null || code == null || !userRegisterInfo.getCode().equals(code)) {
            dataVo.setCode(202);
            dataVo.setMsg("验证码不正确");
        }
        else {
            dataVo = userService.userRegister(userRegisterInfo);
        }
        return dataVo;
    }

    @GetMapping("/getUserInfoByPhone")
    @ApiOperation(value = "通过phone获取用户信息")
    public DataVo getUserInfoByPhone(@RequestParam("phone") String phone) {
        return userService.getUserInfoByPhone(phone);
    }

    @GetMapping("/getUserInfoByUserId")
    @ApiOperation(value = "通过userId获取用户信息")
    public DataVo getUserInfoByUserId(@RequestParam("userId") String userId) {
        return userService.getUserInfoByUserId(userId);
    }

    @PostMapping("/updateInfo")
    @ApiOperation(value = "修改用户信息")
    public DataVo userUpdateInfo(@RequestBody UserInfo userInfo) {
        return userService.userUpdateInfo(userInfo);
    }

    @GetMapping("/getMyGoods")
    @ApiOperation(value = "查询我的闲置")
    public DataVo getMyGoods(String userId, Integer pageNum, Integer pageSize, String query) {
        return userService.getMyGoods(userId, pageNum, pageSize, query);
    }

    @GetMapping("/getMyTransactions")
    @ApiOperation(value = "查询我的订单")
    public DataVo getMyTransactions(String userId, Integer pageNum, Integer pageSize, String query) {
        return userService.getMyTransactions(userId, pageNum, pageSize, query);
    }
}
